<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/16
 * Time: 15:49
 */

class Procurement extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('procurement_model');
        $this->load->model('material_model');
    }
    public function index()
    {
        $dp = gp();
        //对post过来的数组键值进行匹配，判断数据格式是否正确
        $arrdata=array('sku','name_cn','number','price','mt_site','addname','arrival_time','accessory_cost','operator','status');
        if($dp) {
            foreach ($dp as $key => $val) {
                if(is_numeric($key)) {
                    $sss=array_diff(array_keys($val), $arrdata);
                    if (!$sss) {
                        //$val['status']=1;
                        $ruls = $this->procurement_model->updatepcm($key, $val);
                        if($val['sku']) {
                            $this->material_model->updatewuliao($key, array('sku' => $val['sku'], 'accessory_price' => $val['price'], 'accessory_cost' => $val['accessory_cost']));
                        }
                        if ($ruls) {
                            $shit[]=array('reslut' => 'true---ID:' . $key);
                        } else {
                            print_r(array('error message' => 'SQL error!---' . $key));
                        }
                    } else {
                        echo 'error message :传入的数组格式不正确!数据键格式请参照文档！！！';
                        foreach($sss as $key=>$val){
                            $str[]=$val;
                        }
                        print_r("Illegal key".json_encode($str));
                    }
                }else{
                    echo 'error message :传入的数组格式不正确!起始键应为ID值(int类型)！';
                }
            }
            if($shit) {
                print_r(json_encode($shit));
            }
        }else{
            $data = $this->procurement_model->selectmaterialpcm();
            if ($data) {
                echo json_encode($data);
            } else {
                echo json_encode(array('error message' => 'data is null!'));
            }
        }
    }
}